package com.dyzx.hbb.modules.finance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.utils.Utils;
import com.dyzx.hbb.modules.finance.entity.FinanceOrganWallet;
import com.dyzx.hbb.modules.finance.entity.FinanceOrganWalletBill;
import com.dyzx.hbb.modules.finance.mapper.FinanceOrganWalletBillMapper;
import com.dyzx.hbb.modules.finance.service.FinanceOrganWalletBillService;
import com.dyzx.hbb.modules.finance.service.FinanceOrganWalletService;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Administrator
 * @description 针对表【finance_organ_wallet_bill(企业钱包账单表)】的数据库操作Service实现
 * @createDate 2025-07-17 12:12:19
 */
@Service
public class FinanceOrganWalletBillServiceImpl extends ServiceImpl<FinanceOrganWalletBillMapper, FinanceOrganWalletBill>
        implements FinanceOrganWalletBillService {
    @Autowired
    private FinanceOrganWalletService walletService;

    @Override
    public boolean validAppervalIdByBill(Long approvalId) {
        MyLambdaQueryWrapper<FinanceOrganWalletBill> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(FinanceOrganWalletBill::getApprovalId, approvalId);
        return this.count(queryWrapper) > 0;
    }

    @Override
    public boolean walletBillOut(FinanceOrganWalletBill bill2, FinanceOrganWallet validBlance) {
        Long balance = validBlance.getBalance();
        balance = balance - bill2.getAmount();
        validBlance.setBalance(balance);
        bill2.setBalance(balance);
        bill2.setState(2);
        boolean save = this.save(bill2);
        if (save) {
            boolean b = walletService.updateById(validBlance);
            if (b) {
                return true;
            } else {
                throw new RuntimeException("提交失败");
            }
        }
        return false;
    }
@Transactional
    @Override
    public boolean walletBillIn(FinanceOrganWalletBill data) {
        FinanceOrganWallet wallet = getWallet(data);
        data.setWalletId(wallet.getId());
        data.setBalance(wallet.getBalance());
        data.setState(1);
        data.setBalanceOld(wallet.getBalance() - data.getAmount());
        return this.save(data);
    }
    @Transactional

    @Override
    public boolean inWalletBill(Long orderId, Integer type, Long paid, Long pending, boolean isPaid) {
        Long organId = SecurityContextHolder.getOrganId();
        Long userId = SecurityContextHolder.getUserId();
        FinanceOrganWalletBill data = new FinanceOrganWalletBill();
        data.setCreateDate(System.currentTimeMillis());
        data.setUpdateDate(System.currentTimeMillis());
        data.setSalesmanId(userId);
        data.setOrganId(organId);
        data.setOperatorId(userId);

        String no = Utils.generateYyMMOrderNumber("FB");
        data.setNo(no);
        data.setSourceId(orderId);
        data.setSourceType(type);
        data.setAmount(paid);
        data.setCategoryId(1L);
        boolean b = walletBillIn(data);
        if (b) {
          /*  if (!isPaid) {
                String no1 = Utils.generateYyMMOrderNumber("FB");
                data.setNo(no1);
                data.setId( null);
                FinanceOrganWallet walletInfo = getWalletInfo();
                data.setWalletId(walletInfo.getId());
                data.setBalance(walletInfo.getBalance());
                data.setAmount(pending);
                data.setCategoryId(1L);
                return this.save(data);

            } else {
                return true;
            }*/
            return true;
        }

        return false;
    }

    private FinanceOrganWallet getWallet(FinanceOrganWalletBill data) {
        MyLambdaQueryWrapper<FinanceOrganWallet> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(FinanceOrganWallet::getOrganId, data.getOrganId());
        queryWrapper.last("limit 1");
        FinanceOrganWallet one = walletService.getOne(queryWrapper);
        if (one == null) {
            one = new FinanceOrganWallet();
            one.setOrganId(data.getOrganId());
            one.setOperatorId(data.getOperatorId());
            one.setCreateDate(System.currentTimeMillis());
            one.setUpdateDate(System.currentTimeMillis());
            one.setBalance(data.getAmount());
            walletService.save(one);
            return one;

        } else {
            one.setBalance(one.getBalance() + data.getAmount());
            walletService.updateById(one);
            return one;
        }


    }

    private FinanceOrganWallet getWalletInfo() {
        Long organId = SecurityContextHolder.getOrganId();
        Long userId = SecurityContextHolder.getUserId();
        MyLambdaQueryWrapper<FinanceOrganWallet> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(FinanceOrganWallet::getOrganId, organId);
        queryWrapper.last("limit 1");
        FinanceOrganWallet one = walletService.getOne(queryWrapper);
        if (one == null) {
            one = new FinanceOrganWallet();
            one.setOrganId(organId);
            one.setOperatorId(userId);
            one.setCreateDate(System.currentTimeMillis());
            one.setUpdateDate(System.currentTimeMillis());
            one.setBalance(0L);
            walletService.save(one);
            return one;

        } else {
            return one;
        }


    }


}




